package demo2;

public class Solution {

    public int uniquePaths(int m, int n) {

        int[][] dp = new int[m + 1][n + 1];
        dp[0][1] = 1;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m][n];
    }
    public int uniquePaths2(int m, int n) {

        int[][] dp = new int[m][n];
        for(int i = 0; i < m; i++) { dp[i][0] = 1;}
        for(int j = 0; j < n; j++) { dp[0][j] = 1;}

        for(int i = 1; i < m; i++){
            for(int j = 1; j < n; j++){
                dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
            }
        }
        return dp[m - 1][n - 1];
    }


    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int m = obstacleGrid.length, n = obstacleGrid[0].length;

        int[][] dp = new int[m + 1][n + 1];
        dp[0][1] = 1;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                if(obstacleGrid[i - 1][j - 1] != 1){
                    dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
                }
            }
        }
        return dp[m][n];
    }


    public int jewelleryValue(int[][] frame) {
        int n = frame.length, m = frame[0].length;

        int[][] dp = new int[n + 1][m + 1];
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= m; j++){
                dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]) + frame[i - 1][j - 1];
            }
        }
        return dp[n][m];
    }


    public int minFallingPathSum(int[][] matrix) {
        int n = matrix.length;

        int[][] dp = new int[n + 1][n + 2];
        for (int i = 1; i <= n; i++) {
            dp[i][0] = Integer.MAX_VALUE;
            dp[i][n + 1] = Integer.MAX_VALUE;
        }

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                dp[i][j] = Math.min(dp[i - 1][j - 1], Math.min(dp[i - 1][j], dp[i - 1][j + 1])) + matrix[i - 1][j - 1];
            }
        }

        int ret = Integer.MAX_VALUE;
        for (int j = 1; j <= n; j++) {
            ret = Math.min(ret, dp[n][j]);
        }
        return ret;
    }


    public int minPathSum(int[][] grid) {
        int m = grid.length, n = grid[0].length;

        int[][] dp = new int[m + 1][n + 1];
        for(int i = 0; i <= m; i++) { dp[i][0] = Integer.MAX_VALUE;}
        for(int j = 0; j <= n; j++) { dp[0][j] = Integer.MAX_VALUE;}
        dp[0][1] = 0;

        for(int i = 1; i <= m; i++){
            for(int j = 1; j <= n; j++){
                dp[i][j] = Math.min(dp[i - 1][j], dp[i][j - 1]) + grid[i - 1][j - 1];
            }
        }
        return dp[m][n];
    }


    public int calculateMinimumHP(int[][] dungeon) {
        int m = dungeon.length, n = dungeon[0].length;

        int[][] dp = new int[m + 1][n + 1];
        for(int i = m; i >= 0; i--) { dp[i][n] = Integer.MAX_VALUE;}
        for(int j = n; j >= 0; j--) { dp[m][j] = Integer.MAX_VALUE;}
        dp[m][n - 1] = 1;

        for(int i = m - 1; i >= 0; i--){
            for(int j = n - 1; j >= 0; j--){
                dp[i][j] = Math.min(dp[i + 1][j], dp[i][j + 1])  - dungeon[i][j];
                dp[i][j] = Math.max(dp[i][j], 1);
            }
        }
        return dp[0][0];
    }
}
